package com.schooldate.model.service.impl;

import com.schooldate.base.BaseSupportImpl;
import com.schooldate.model.entity.Department;
import com.schooldate.model.entity.School;
import com.schooldate.model.service.DepartmentService;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by etund on 2015/3/16 0016.
 */
@Service
@Transactional
@SuppressWarnings("all")
public class DepartmentServiceImpl extends BaseSupportImpl<Department> implements DepartmentService {

    @Override
    public List<Department> getBySchoolId(String schoolId) {
        list =  this.getSession().createCriteria(Department.class)
                        .add(Restrictions.eq("sid",schoolId))
                        .list();
        return list;
    }

    @Override
    public List<Department> getBydepartmentNames(String[] names,String schoolId) {
        String sql = "from Department d where ";
        if (names.length>0){
            sql += " d.name like '";
            for (String name:names){
                if (!name.trim().equals("")){
                    sql += "%"+name;
                }
            }
            sql += "%' and sid = "+schoolId;
        }
        System.out.println(sql+"===================================");
        return getSession().createQuery(sql).list();
    }

    @Override
    public Department getBySchoolIdAndName(String sid, String departmentId) {

        return (Department)this.getSession().createCriteria(Department.class)
                .add(Restrictions.eq("sid",sid))
                .add(Restrictions.eq("name",departmentId))
                .list().get(0);
    }

    @Override
    public Department getByName(String departmentId) {
        list = this.getSession().createCriteria(Department.class)
                .add(Restrictions.eq("name",departmentId))
                .list();
        if (list.size()>0){
            return list.get(0);
        }
        return null;
    }
}
